package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Include;
import org.zkoss.zul.Textbox;

import com.lt.flashcard.model.entity.Flashcard;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.FlashcardService;
import com.lt.flashcard.model.service.Repository;

public class AdminFlashcardSetDetailsCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
	private String flashcardsetId;
	private FlashcardSet flashcardset;
	private List<Flashcard> flashcards;
	
	@Wire
	private Textbox fcTerm;
	
	@Wire
	private Include ctnFcList;

	
	/* (non-Javadoc)
	 * @see org.zkoss.zk.ui.select.SelectorComposer#doBeforeCompose(org.zkoss.zk.ui.Page, org.zkoss.zk.ui.Component, org.zkoss.zk.ui.metainfo.ComponentInfo)
	 */
	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		// TODO Auto-generated method stub
		Object obj = Executions.getCurrent().getParameter("fcsid");
		AAService srv = Repository.getInstance().getAAService();
		if (!srv.isLogin() || !srv.hasRole("admin")) {
			Executions.sendRedirect("/admin_login.zul");
		}
		if(obj != null) {
			flashcardsetId = String.valueOf(obj);
			flashcardset = Repository.getInstance().getFlashcardSetService().findByID(Long.parseLong(flashcardsetId));
			flashcards = flashcardset.getFlashcards();
		}
		return super.doBeforeCompose(page, parent, compInfo);
	}

	/* (non-Javadoc)
	 * @see org.zkoss.zk.ui.select.SelectorComposer#doBeforeComposeChildren(org.zkoss.zk.ui.Component)
	 */
	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		// TODO Auto-generated method stub
		
		super.doBeforeComposeChildren(comp);
	}
	
	/**
	 * @return the flashcardsetId
	 */
	public String getFlashcardsetId() {
		return flashcardsetId;
	}

	/**
	 * @param flashcardsetId the flashcardsetId to set
	 */
	public void setFlashcardsetId(String flashcardsetId) {
		this.flashcardsetId = flashcardsetId;
	}

	/**
	 * @return the flashcardset
	 */
	public FlashcardSet getFlashcardset() {
		return flashcardset;
	}

	/**
	 * @param flashcardset the flashcardset to set
	 */
	public void setFlashcardset(FlashcardSet flashcardset) {
		this.flashcardset = flashcardset;
	}
	
	/**
	 * @return the ctnFcList
	 */
	public Include getCtnFcList() {
		return ctnFcList;
	}

	/**
	 * @param ctnFcList the ctnFcList to set
	 */
	public void setCtnFcList(Include ctnFcList) {
		this.ctnFcList = ctnFcList;
	}

	/**
	 * @return the fcTerm
	 */
	public Textbox getFcTerm() {
		return fcTerm;
	}

	/**
	 * @param fcTerm the fcTerm to set
	 */
	public void setFcTerm(Textbox fcTerm) {
		this.fcTerm = fcTerm;
	}

	/**
	 * @return the flashcards
	 */
	public List<Flashcard> getFlashcards() {
		return flashcards;
	}

	/**
	 * @param flashcards the flashcards to set
	 */
	public void setFlashcards(List<Flashcard> flashcards) {
		this.flashcards = flashcards;
	}

	@Listen("onClick=#btnFilterFc")
	public void filterFlashcards(){
		FlashcardService fsrv = Repository.getInstance().getFlashcardService();
		flashcards = fsrv.filterFlashcard(fcTerm.getValue(), flashcardset.getId());
		getCtnFcList().invalidate();
	}

}
